<template>
  <div class="container">
    <div class="icon">
      <img src="./images/icon.png" alt />
    </div>
    <div class="title">登录/注册易卡拉权益</div>
    <div class="login">
      <van-form
        @submit="onSubmit"
        ref="form"
        :validate-first="true"
        @failed="onFailed"
        :show-error-message="false"
      >
        <!-- :show-error-message="false" -->
        <van-field
          v-model="mobile"
          name="mobile"
          label="手机号码"
          placeholder="请输入手机号码"
          :rules="[
            { required: true, message: '请填写手机号码' },
            {
              pattern: /^1[3456789]\d{9}$/,
              message: '请填写正确手机号码',
            },
          ]"
        />
        <van-field
          v-model="dynamicCode"
          name="dynamicCode"
          label="短信验证码"
          placeholder="请输入验证码"
          :rules="[{ required: true, message: '请填写验证码' }]"
        >
          <template slot="right-icon">
            <div class="getCode" @click="getCode" v-if="!hasSendCode">
              获取验证码
            </div>
            <div class="sendCode" v-else>{{ second }}s</div>
          </template>
        </van-field>
        <div style="margin: 16px">
          <van-button
            color="#1E1F26"
            round
            block
            type="info"
            native-type="submit"
          >
            登录
          </van-button>
          <div class="regist" @click="passwordLogin">密码登录</div>
          <div class="tips">
            <div @click="forgetPw">忘记密码</div>
            <div @click="applyIn">申请入驻</div>
          </div>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { sendMobileCode_post, login_post } from '@/api/app/seller/login'
import { Toast } from 'vant'
export default {
  data() {
    return {
      mobile: '',
      dynamicCode: '',
      hasSendCode: false,
      second: 60,
      timer: null,
    }
  },
  methods: {
    onFailed(values) {
      if (Array.isArray(values.errors)) {
        Toast.fail(values.errors[0].message)
      }
    },
    onSubmit(values) {
      login_post({
        data: { ...values, appType: 'web' },
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      }).then((data) => {
        this.$notify({
          message: '登录成功',
          type: 'success',
          onClose: () => {
            this.$router.push('/index')
          },
        })
      })
    },
    forgetPw() {
      this.$router.push('/forgetpw')
    },
    getCode(e) {
      this.$refs.form.validate('mobile').then(
        () => {
          sendMobileCode_post({
            data: {
              mobile: this.mobile,
              appType: 'web',
              challenge: '111111',
            },
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          }).then((data) => {
            this.hasSendCode = true
            this.timer = setInterval(() => {
              this.second--
              if (this.second === 0) {
                this.hasSendCode = false
                clearInterval(this.timer)
                this.timer = null
                this.second = 60
              }
            }, 1000)
          })
        },
        (err) => {
          console.log(err)
        }
      )
    },
    passwordLogin() {
      this.$router.push('/login')
    },
    applyIn() {
      this.$router.push('/apply')
    },
  },
}
</script>

<style lang="scss" src="./styles/index.scss" scoped></style>
